<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title></title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="../../../layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="../../../layuiadmin/style/admin.css" media="all">
</head>
<body>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-form layui-card-header layuiadmin-card-header-auto" lay-filter="search_from">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">名称</label>
                            <div class="layui-input-inline">
                                <input type="text" name="title" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商家</label>
                            <div class="layui-input-inline">
                                <script type="text/html" template lay-url="{{ layui.setter.apiHost }}/seller/seller/select" lay-done="layui.form.render();">
                                    <select name="seller_id">
                                        <option value=""></option>
                                        {{# layui.each(d.data, function(index, item){ }}
                                        <option value="{{ index }}" {{# if(index== d.params.seller_id){ }}selected{{# } }}>{{ item }}</option>
                                        {{# }); }}
                                    </select>
                                </script>
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button class="layui-btn layuiadmin-btn-list" lay-submit lay-filter="search_button">
                                <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                            </button>
                        </div>
                    </div>
                </div>

                <script type="text/html" id="toolbar_tmp">
                    <div class="layui-btn-container">
                        <button class="layui-btn layui-btn-sm" lay-event="add">添加</button>
                        <button class="layui-btn layui-btn-sm" lay-event="status_on">审核</button>
                        <button class="layui-btn layui-btn-sm" lay-event="status_off">锁定</button>
                        <button class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del">删除</button>
                    </div>
                </script>

                <div class="layui-card-body">
                    <table id="table_list" lay-filter="table_list"></table>
                    <script type="text/html" id="open_default_tmp">
                        <span class="layui-badge {{# if (d.open_default == '启用'){ }}layui-bg-green{{# }else{ }}layui-bg-orange{{# } }}">{{ d.open_default }}</span>
                    </script>
                    <script type="text/html" id="price_type_tmp">
                        <span class="layui-badge {{# if (d.price_type == '全国'){ }}layui-bg-green{{# }else{ }}layui-bg-orange{{# } }}">{{ d.price_type }}</span>
                    </script>
                    <script type="text/html" id="status_tmp">
                        <input type="checkbox" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定" lay-filter="status_btn" {{#
                               if(d.status== 1){ }}checked{{# } }}>
                    </script>
                    <script type="text/html" id="action_button">
                        <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<script src="../../../layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '../../../layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table', 'helper'], function () {
        var admin = layui.admin,
            table = layui.table,
            form = layui.form,
            helper = layui.helper,
            edit_view_url = 'goods/delivery/add',//添加编辑页模板地址
            api_action_url = layui.setter.apiHost + '/goods/delivery';

        //表格操作
        table.render({
            elem: '#table_list',
            url: api_action_url,
            toolbar: '#toolbar_tmp',
            cols: [[
                {type: 'checkbox', fixed: 'left'},
                {field: 'id', title: 'ID', width: 80},
                {field: 'title', title: '名称', minWidth: 200},
                {field: 'open_default', title: '默认运费', minWidth: 120, templet: '#open_default_tmp'},
                {field: 'price_type', title: '费用类型', minWidth: 120, templet: '#price_type_tmp'},
                {field: 'status', title: '状态', width: 100, templet: '#status_tmp'},
                {field: 'created_at', title: '创建时间', width: 170},
                {title: '操作', width: 150, align: 'center', fixed: 'right', toolbar: '#action_button'}
            ]],
            page: true
        });

        //头部工具栏操作
        table.on('toolbar(table_list)', function (obj) {
            var type = obj.event;
            switch (type) {
                case 'add':
                    helper.open_window('添加', api_action_url, edit_view_url, {width: '100%'});
                    break;
                case 'status_on':
                    helper.ajax(api_action_url + '/status', {status: 1});
                    break;
                case 'status_off':
                    helper.ajax(api_action_url + '/status', {status: 0});
                    break;
                case 'del':
                    layer.confirm('确定删除吗', function (index) {
                        helper.ajax(api_action_url + '/delete');
                    });
                    break;
            }
        });

        //监听工具条操作按钮
        table.on('tool(table_list)', function (obj) {
            var data = obj.data;
            switch (obj.event) {
                case 'edit':
                    helper.open_window('编辑', api_action_url, edit_view_url, {id: data.id, width: '100%'});
                    break;
                case 'del':
                    layer.confirm('确定删除吗', function (index) {
                        if (helper.ajax(api_action_url + '/delete', {id: data.id}, false)) {
                            obj.del();
                            layer.close(index);
                        }
                    });
                    break;
            }
        });

        //监听锁定操作
        form.on('switch(status_btn)', function (obj) {
            var send_data = {id: this.value, status: obj.elem.checked == true ? 1 : 0};
            helper.ajax(api_action_url + '/status', send_data, false);
        });

        //监听单元格编辑
        table.on('edit(table_list)', function (obj) {
            var send_data = {id: obj.data.id, field: obj.field, field_value: obj.value};
            helper.ajax(api_action_url + '/field_update', lay_id_prefix, send_data, false);
        });
    });
</script>
</body>
</html>